package com.yingxin.yxerp.module.sys.controller.other;

import com.yingxin.yx.framework.commons.id.IdHelper;
import com.yingxin.yxerp.module.sys.entity.OtherSet;
import com.yingxin.yxerp.module.sys.entity.enums.DictionaryType;
import com.yingxin.yxerp.module.sys.service.IOtherSetService;
import com.yingxin.yxerp.module.sys.vo.req.OtherSetCreateVo;
import com.yingxin.yxerp.module.sys.vo.req.OtherSetQueryVo;
import com.yingxin.yxerp.module.sys.vo.resp.OtherSetRespVo;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 币种管理 前端控制器
 * @author yuebao
 * @since 2025-03-28
 */

@RestController
@RequestMapping("/sys/currency")
public class CurrencyController {

    @Autowired
    private IOtherSetService otherSetService;

    @ApiOperation(value = "查询列表-全部")
    @PostMapping("/list")
    public List<OtherSetRespVo> list(@RequestBody OtherSetQueryVo queryVo) {
        queryVo.setOtherType(DictionaryType.CURRENCY.getType());
        return otherSetService.listVo( queryVo );
    }

    @ApiOperation(value = "根据id查询")
    @GetMapping("/{id}")
    public OtherSetRespVo get(@PathVariable("id") String id) {
        return otherSetService.getVo( id );
    }


    @PreAuthorize("hasAuthority('system:currency:save')")
    @ApiOperation(value = "保存")
    @PostMapping("/save")
    public String save(@RequestBody @Valid OtherSetCreateVo createVo) {
        OtherSet otherSet = new OtherSet();
        BeanUtils.copyProperties(createVo,otherSet);
        otherSet.setId( IdHelper.snowflakeId() );
        otherSet.setType( DictionaryType.CURRENCY.getType() );
        otherSetService.create( otherSet );
        return otherSet.getId();
    }

    @PreAuthorize("hasAuthority('system:currency:delete')")
    @ApiOperation(value = "删除")
    @DeleteMapping("/{id}")
    public void delete(@PathVariable String id) {
        otherSetService.delete( id );
    }

    @PreAuthorize("hasAuthority('system:currency:update')")
    @ApiOperation(value = "编辑")
    @PutMapping("/update")
    public void update(@RequestBody @Valid OtherSetCreateVo createVo) {
        OtherSet otherSet = new OtherSet();
        BeanUtils.copyProperties(createVo,otherSet);
        otherSetService.update( otherSet );
    }

}
